

<!DOCTYPE html>
<html lang="en" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" type="image/png" href="/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  <title>第一篇 - Hexo</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10.4.0/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.8.7","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"onlypost":false},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null}}};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.3.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>Fluid</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                Home
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                Archives
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                Categories
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                Tags
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                About
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="第一篇">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-01-07 19:42" pubdate>
        January 7, 2021 pm
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      789 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      9
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">第一篇</h1>
            
            <div class="markdown-body">
              <h1 id="文件"><a href="#文件" class="headerlink" title="文件"></a>文件</h1><h2 id="流"><a href="#流" class="headerlink" title="流"></a>流</h2><table>
<thead>
<tr>
<th>文件指针</th>
<th>流</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>stdin</td>
<td>标准输入流</td>
<td>使用键盘输入</td>
</tr>
<tr>
<td>stdout</td>
<td>标准输出流</td>
<td>输出到屏幕上</td>
</tr>
</tbody></table>
<h2 id="文件指针"><a href="#文件指针" class="headerlink" title="文件指针"></a>文件指针</h2><p>类型为<code>FILE *</code></p>
<h3 id="使用方法"><a href="#使用方法" class="headerlink" title="使用方法"></a>使用方法</h3><p>*<em>FILE <em>变量</em></em></p>
<p><code>FILE *fp</code></p>
<h2 id="文件操作"><a href="#文件操作" class="headerlink" title="文件操作"></a>文件操作</h2><h3 id="打开文件"><a href="#打开文件" class="headerlink" title="打开文件"></a>打开文件</h3><p>使用<code>fopen</code>函数</p>
<p>####用法：</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs c">FILE *fp=fopen(<span class="hljs-string">&quot;路径&quot;</span>,<span class="hljs-string">&quot;模式&quot;</span>)<br></code></pre></td></tr></table></figure>
<p>路径中如果包含<code>\</code>时，默认在字符串里将被当作转义字符的开头对待，可以写成<code>\\</code>或者<code>/</code></p>
<p><strong>例如</strong></p>
<p>要打开<code>C:\mingw64\build-info.txt</code>文件时，可写为<code>C:\\mingw64\\build-info.txt</code>或者<code>C:/mingw64/build-info.txt</code></p>
<h4 id="模式"><a href="#模式" class="headerlink" title="模式"></a>模式</h4><p>针对文本文件</p>
<table>
<thead>
<tr>
<th align="center">字符串</th>
<th align="left">含义</th>
</tr>
</thead>
<tbody><tr>
<td align="center">r</td>
<td align="left">仅读取</td>
</tr>
<tr>
<td align="center">w</td>
<td align="left">写入文件，若文件不存在则自动创建，如果存在，则(从头，即当前位置)直接替换全部内容</td>
</tr>
<tr>
<td align="center">a</td>
<td align="left">追加文本，默认是追加到尾部，若文件不存在，则自动创建</td>
</tr>
<tr>
<td align="center">r+</td>
<td align="left">读写，从头部开始替换，文件不存在不会自动创建</td>
</tr>
<tr>
<td align="center">w+</td>
<td align="left">用于读写，若文件存在则替换全部内容，不存在则自动创建</td>
</tr>
<tr>
<td align="center">a+</td>
<td align="left">用于读写，若文件存在就追加到尾部，若不存在则创建</td>
</tr>
</tbody></table>
<p>#####r+的替换</p>
<p>若之前文件内容为</p>
<figure class="highlight dns"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs dns"><span class="hljs-number">123456789</span><br></code></pre></td></tr></table></figure>
<p>执行下列代码</p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs c">FILE *fp=fopen(<span class="hljs-string">&quot;1.txt&quot;</span>,<span class="hljs-string">&quot;r+&quot;</span>);<br><span class="hljs-built_in">fprintf</span>(fp,<span class="hljs-string">&quot;99999&quot;</span>);<br></code></pre></td></tr></table></figure>
<p>之后，文件内容将变成</p>
<figure class="highlight dns"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs dns"><span class="hljs-number">999996789</span><br></code></pre></td></tr></table></figure>
<p>使用**r+  w+   a+**进行文件读写时尽量只执行其中的一项，及仅读或者仅写，因为文件指针会来回变化的，读写文件后，文件内的流指针会停在当前位置。</p>
<p>针对一上问题，有两种解决办法。</p>
<p>第一种 重新打开文件</p>
<p>第二种 使用文件定位函数</p>
<h4 id="文件定位"><a href="#文件定位" class="headerlink" title="文件定位"></a>文件定位</h4><h4 id="seek"><a href="#seek" class="headerlink" title="seek"></a>seek</h4><p>调用形式<code>fseek(流,字节数,位置)</code></p>
<p><strong>字节数</strong>为来回推进的字节数，负数代表往回(左)推进的字节数，正数代表往前(右)推进的字节数。</p>
<p><strong>位置</strong>为从哪个位置进行推进，通常有三种类型</p>
<table>
<thead>
<tr>
<th align="center">类型</th>
<th align="center">说明</th>
</tr>
</thead>
<tbody><tr>
<td align="center">SEEK_SET</td>
<td align="center">起始处</td>
</tr>
<tr>
<td align="center">SEEK_CUR</td>
<td align="center">当前位置</td>
</tr>
<tr>
<td align="center">SEEK_END</td>
<td align="center">末尾处</td>
</tr>
</tbody></table>
<p>返回到头部可写为<code>fseek(fp,0L,SEEK_SET)</code></p>
<p>转到尾部可以写为<code>fseek(fp,0L,SEEK_END)</code></p>
<h4 id="rewind"><a href="#rewind" class="headerlink" title="rewind"></a>rewind</h4><p>将文件指针返回到头部</p>
<p>用法<code>rewind(fp)</code></p>
<p>等价于<code>fseek(fp,0L,SEEK_SET)</code></p>
<h4 id="关闭文件"><a href="#关闭文件" class="headerlink" title="关闭文件"></a>关闭文件</h4><p><code>fclose(fp)</code></p>
<h2 id="输入输出"><a href="#输入输出" class="headerlink" title="输入输出"></a>输入输出</h2><h3 id="输入"><a href="#输入" class="headerlink" title="输入"></a>输入</h3><h4 id="fscanf"><a href="#fscanf" class="headerlink" title="fscanf"></a>fscanf</h4><p>可以在文件/标准输入流<strong>stdin</strong>中读入</p>
<p>格式<code>fscanf(流,&quot;格式&quot;,接收变量)</code></p>
<p>流可以设置为文件，当设置为<strong>stdin</strong>时，作用效果与scanf一致</p>
<hr>
<h4 id="fgets"><a href="#fgets" class="headerlink" title="fgets"></a>fgets</h4><p>可在文件/标准输入流中读入，可以解决gets不安全问题</p>
<p>格式<code>fgets(变量,字节数,流)</code></p>
<figure class="highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs c">fgets(str,<span class="hljs-keyword">sizeof</span>(str),<span class="hljs-built_in">stdin</span>);<br></code></pre></td></tr></table></figure>
<p>当遇到换行或者已经读入的字符达到<code>sizeof(str)</code>-1个字符时将停止读入</p>
<hr>
<h4 id="fgetc"><a href="#fgetc" class="headerlink" title="fgetc"></a>fgetc</h4><p>可在文件/标准输入流中读入</p>
<p>格式<code>变量=getc(流)</code></p>
<p>当<code>流</code>为<code>stdin</code>时效果与<code>getchar()</code>一致</p>
<hr>
<h3 id="输出"><a href="#输出" class="headerlink" title="输出"></a>输出</h3><h4 id="fprintf"><a href="#fprintf" class="headerlink" title="fprintf"></a>fprintf</h4><p>格式<code>fprintf(流,&quot;格式&quot;,变量)</code></p>
<p>流可以设置为文件，当设置为<strong>stdout</strong>时，作用效果与<code>printf</code>一致</p>
<hr>
<h4 id="fputs"><a href="#fputs" class="headerlink" title="fputs"></a>fputs</h4><p>格式<code>fputs(&quot;内容&quot;/字符变量,流)</code></p>
<p>当流为<code>stdout</code>时，与<code>puts</code>效果一致</p>
<hr>
<h4 id="fputc"><a href="#fputc" class="headerlink" title="fputc"></a>fputc</h4><p>格式<code>fputc(变量/&#39;字符&#39;,流)</code></p>
<p>当流为<code>stdout</code>时，与<code>putchar</code>效果一致</p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
              </div>
              
                <p class="note note-warning">本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！</p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/01/07/%E7%AC%AC%E4%B8%80%E7%AF%87%E5%8D%9A%E5%AE%A2/">
                        <span class="hidden-mobile"></span>
                        <span class="visible-mobile">Next</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;TOC</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">Search</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">keyword</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>

<!-- SCRIPTS -->

  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/lazyload.js" ></script>
  



  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.0/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.0/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js" ></script>






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.11/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    (function () {
      var path = "/local-search.xml";
      var inputArea = document.querySelector("#local-search-input");
      inputArea.onclick = function () {
        searchFunc(path, 'local-search-input', 'local-search-result');
        this.onclick = null
      }
    })()
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>



</body>
</html>
